package cn.com.dycc.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.dycc.pagepermission.dopermis.dao.DoDao;
import cn.com.dycc.pagepermission.dopermis.param.DoParam;
import cn.com.dycc.sys_manage.usersysmanagement.service.UserService;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PropertiesUtil;


public class DoFilter implements Filter{
	
	private DoDao doDao;
	private UserService userService;
	
	public void destroy() {
			
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		if (request.getCharacterEncoding() == null) {  
			request.setCharacterEncoding("UTF-8");  
        }	
		
		
		
		String uri = request.getRequestURI();
		
//		System.out.println("do="+uri);
		String path = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
		
		//获取消息头
		String mes_title = request.getHeader("user-agent");
		if(mes_title.contains("okhttp/3.6.0")
				||mes_title.contains("Android")
				||mes_title.contains("iPhone")
				||mes_title.contains("iPad")){ //安卓放行
			
			HttpSession session = request.getSession();
			session.setMaxInactiveInterval(3*60);
			System.out.println("path"+path);
			if("/login".equals(path)){
				
				chain.doFilter(request, response);
				return;
			}
			String userId = (String) session.getAttribute("id");
			
			if(userId==null){
				String agent = request.getHeader("user-agent");
				
				if(agent.contains("okhttp/3.6.0")||agent.contains("Android")
						||agent.contains("iPhone")
						||agent.contains("iPad")
						||agent.contains("mui")){
					String account = request.getHeader("ac");
					String password = request.getHeader("pd");
					System.out.println("a="+account+"p="+password);

					try {
						boolean b = userService.autoLogin(account, password, request);
						
						if(b){
							chain.doFilter(request, response);
							return;
						}
					} catch (Exception e) {
						return;
						// TODO: handle exception
					}
				}
			}
			chain.doFilter(request, response);
			return;
		}
		
		//pc端
		if("/login".equals(path)){
			chain.doFilter(request, response);
			return;
		}else if("/aotoLogin".equals(path)){
			chain.doFilter(request, response);
			return;
		}else if("/code".equals(path)){
			chain.doFilter(request, response);
			return;
		}
		
		HttpSession pc_session = request.getSession();
		pc_session.setMaxInactiveInterval(3*60);
		
//		System.out.println("cok="+session.getId());
		
		String pc_userId = (String) pc_session.getAttribute("id");
		
		if(pc_userId==null){			
//			System.out.println("6......");
			response.sendRedirect("/login.html");			
		}
//		else{
////			System.out.println("5......");
//			
//			//放行do请求
//			String open_do = PropertiesUtil.readProperties("conf/openDo.properties","open_do");
//			String[] arr_open_do = open_do.split(",");
//			for(String od : arr_open_do){
//				//System.out.println("od="+od);
//				if(uri.equals(od)){
//					chain.doFilter(request, response);
//					return;
//				}
//			}
//			
//			String org_id = request.getSession().getAttribute("org_id").toString();
//			DoParam doParam = new DoParam();
//			doParam.setLimit_flag(1);
//			doParam.setInterface_url(uri);
////			doParam.setOrg_id(org_id);
//			
//			List<String> list_role = doDao.findDoPath(doParam);
////			System.out.println("list_role="+list_role);
//			
//			String list_role2 = request.getSession().getAttribute("role_no").toString();
////			System.out.println("list_role2="+list_role2);
//			
//			String[] arr_role = list_role2.replace("[","").replace("]","").split(",");
////			System.out.println("arr_role="+arr_role.length);
//			
//			for(String role : list_role){
////				System.out.println("r="+role);
//				role = (role==null)?"":role;
//				for(String role1 : arr_role){
////					System.out.println("r1="+role1);
//					if(role1.trim().equals(role.trim())){
////						request.setAttribute("uri",uri);
////						System.out.println("0.....");
//						chain.doFilter(request, response);
//						return;
//					}
//				}
//				
//			}
//			response.sendRedirect("/error.html");			
			chain.doFilter(request, response);
//		}		
	}		
	

	public void init(FilterConfig arg0) throws ServletException {
		
		String[]conf={"conf/spring-mvc.xml","conf/spring-mybatis.xml"};
		ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//		if(ac!=null&&ac.getBean("roleDao")!=null && roleDao==null){
//			roleDao = (RoleDao) ac.getBean("roleDao");
//		}s
		if(ac!=null&&ac.getBean("doDao")!=null && doDao==null){
			doDao = (DoDao) ac.getBean("doDao");
		}
		if(ac!=null&&ac.getBean("userService")!=null&&userService==null){
			userService = (UserService) ac.getBean("userService");
		}
	}

}
